import re
import sys
import tempfile
import os
from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions import interaction
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput
import uuid


class CommonFunction:

    def __init__(self, driver):
        self.driver = driver

    def click_the_point(self, x, y):
        """
        点击
        """
        actions = ActionChains(self.driver)
        actions.w3c_actions = ActionBuilder(self.driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch"))
        actions.w3c_actions.pointer_action.move_to_location(x=x, y=y)
        actions.w3c_actions.pointer_action.pointer_down()
        actions.w3c_actions.pointer_action.pause(0.1)
        actions.w3c_actions.pointer_action.release()
        actions.perform()

    def screen_shoot(self, filename='未指定', path='.'):
        """
        截屏
        """
        self.driver.save_screenshot(f"{path}/{filename}.png")

    def create_dir(self, path):
        """
         创建目录
        """
        os.makedirs(path, exist_ok=True)
        return path

    def get_from_to_location(self, element):
        """
        获取元素的左上角和右下角的坐标
        """
        from_element_location = re.match(r"\[(\d+,\d+)\]\[(\d+,\d+)\]", element.get_attribute("bounds")).group(1).split(',')
        to_element_location = re.match(r"\[(\d+,\d+)\]\[(\d+,\d+)\]", element.get_attribute("bounds")).group(2).split(',')
        from_x, from_y, to_x, to_y = int(from_element_location[0]), int(from_element_location[1]), int(to_element_location[0]), int(to_element_location[1])
        return from_x, from_y, to_x, to_y

    @classmethod
    def is_blank(cls, text: str):
        """
        判断是否是空白字符
        """
        return not text or text.strip() == ""
